package com.future.common.utils;

import com.jfinal.kit.PropKit;

import xyz.downgoon.snowflake.Snowflake;
/**
 * 基于雪花算法的id生成器
 * @author Leon.Zhang
 *
 */
public class IDUtil {
	private static final Snowflake snowflake = new Snowflake(PropKit.getLong("datacenterId"),
			PropKit.getLong("workerId"));;

	// 产生id
	public static String getId() {
		if (snowflake == null) {
			throw new RuntimeException("Please Init snowflake first!");
		}
		return String.valueOf(snowflake.nextId());
	}

	// 格式化id,返回string格式
	// 时间+sequence+datacenter+workerid
	public static String formatId(long id) {
		return snowflake.formatId(id);
	}

	// 返回时间 sequence datacenterid workerid的数组
	public static long[] parseId(long id) {
		return snowflake.parseId(id);
	}
}
